<?php
/**
 *  @copyright 2010
 *  @package _version@
 *  @author oriotiori
 */

class Admin_NewsController extends Zend_Controller_Action
{

    public function init()
    {
        $translate  = Zend_Registry::get('Zend_Translate');
        $this->view->breadCrumbs = $this->view->crumbs();
        $this->view->breadCrumbs->addPrepend('/', $translate->_('Home'))
            ->setMvc(true);

        $this->view->crumbs = $this->view->breadCrumbs->init();
		/* Initialize action controller here */
    }
	
    public function indexAction()
    {
        $this->_redirect($this->_request->getParam('module').'/'. $this->_request->getParam('controller') .'/list');
    }
	
/**
 * Método para agregar o actualizar una Noticia
 * @return void
 */
    public function addAction()
    {
        //$translate  = Zend_Registry::get('Zend_Translate');
        $s_Sass     = new Zend_Session_Namespace('s_Sass');
        $newsFacade = new Admin_Model_Facade_News();
        $ToNews     = new Admin_Model_TO_News();

        $request    = $this->getRequest();
        $x_id       = $this->_request->getParam('Id', 0);
        $catId      = $this->_request->getParam('CatId', 0);

        $ToNews->setCompanyId(isset($s_Sass->CompanyId) ? $s_Sass->CompanyId : 1 );
        $ToNews->setLanguage(isset($s_Sass->language) ? $s_Sass->language : 'es' );
        $ToNews->setCategoryId($catId);
        $ToNews->setModifiedBy(isset($s_Sass->UserId) ? $s_Sass->UserId : 1);

        $form = new Admin_Form_News($ToNews->getAllVars());

        if($request->isPost()){

            if($form->isValid($this->_request->getPost())){

               $aux_seo=$form->getValue('Seo');
                $aux_title=$form->getValue('Title');

                $seo=$newsFacade->convertir_seo((strlen($aux_seo)>0) ? $aux_seo : $aux_title);

                $ToNews->setId($form->getValue('Id'));
                $ToNews->setTitle($form->getValue('Title'));
                $ToNews->setSeo($seo);
                $ToNews->setShortDescription($form->getValue('ShortDescription'));
                $ToNews->setLongDescription($form->getValue('LongDescription'));
                
                if($ToNews->getId()==0){
                    $data = $newsFacade->addNews($ToNews->getAllVars());
                    if($data){
                        $s_Sass->message = $translate->_('News added');
                    }
                }
                else{

                    
                    $data = $newsFacade->updateNews($ToNews->getAllVars());
                    if($data){
                        $s_Sass->message = $translate->_('News Updated');
                    }
                }
                
                $this->_redirect(
                        $this->_request->getParam('module').DIAG_PATH.$this->_request->getParam('controller').DIAG_PATH.'list?CatId='.$catId);
            }
	}

//si no se dio click en submit
        if($x_id>0 && is_numeric($x_id)){
            $ToNews->setId($x_id);
            $data=$newsFacade->getNews($ToNews->getAllVars());
            $form = new Admin_Form_News($data);
            
            $this->view->module= $this->_request->getParam('module');
            $this->view->controller= $this->_request->getParam('controller');
            $this->view->form=$form;
            $this->view->data=$data;
        }
        else
        {
            
            $this->view->module= $this->_request->getParam('module');
            $this->view->controller= $this->_request->getParam('controller');
            $this->view->form=$form;

        }
        $this->view->catId=$catId;
    }

/**
 * Método para listar las categorias de las noticias
 * @return void
 */
    public function listAction()
    {
        $s_Sass         = new Zend_Session_Namespace('s_Sass');
        $FacadeNews  = new Admin_Model_Facade_News();
        $ToNews      = new Admin_Model_TO_News();
                
        $company    = $s_Sass->CompanyId;
        $request    = $this->getRequest();
        $CategoryId = $this->_request->getParam('CatId');

        if (strlen($s_Sass->message)>0){
            
            $this->view->message = $s_Sass->message;
            $s_Sass->message="";
        }

        $ToNews->setCompanyId($company);
        $ToNews->setLanguage($s_Sass->language);
        $ToNews->setCategoryId(($CategoryId) ? $CategoryId : 8);

        $data = $FacadeNews->getAllNews($ToNews->getAllVars());
        
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($data));
        $paginator  ->setItemCountPerPage(12)->setCurrentPageNumber($this->_getParam('page', 1));

        $this->view->paginator = $paginator;
        $this->view->module= $this->_request->getParam('module');
        $this->view->controller= $this->_request->getParam('controller');
        $this->view->catId= $CategoryId;
        
    }
	
/**
 * Método para eliminar Una categoria
 * @return void
 */
    public function deleteAction()
    {
        $FacadeNews=	new Admin_Model_Facade_News();
        $ToNews=		new Admin_Model_TO_News();
        $translate = 	Zend_Registry::get('Zend_Translate');
        $s_Sass = 	new Zend_Session_Namespace('s_Sass');

        $request=$this->getRequest();
        $id=$this->_request->getParam('Id');
        $CategoryId=$this->_request->getParam('CatId');
        $modifico =($s_Sass->UserId != 0)? $s_Sass->UserId : 1;

        $ToNews->setId($id);
        $ToNews->setCategoryId($CategoryId);
        $ToNews->setModifiedBy($modifico);

        $data=$FacadeNews->deleteNews($ToNews);
        if($data){
                $s_Sass->message = $translate->_('New deleted.');
        }

        $this->_redirect($this->_request->getParam('module').DIAG_PATH.$this->_request->getParam('controller').DIAG_PATH.'list?CatId='.$ToNews->getCategoryId());
    }
}